package com.atguigu.udmp.web.userprofile.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.udmp.web.userprofile.bean.DimInfoDefine;
import com.atguigu.udmp.web.userprofile.bean.UserEventDefine;
import com.atguigu.udmp.web.userprofile.service.DimInfoDefineService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 维度定义 前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2023-11-08
 */
@RestController
@RequestMapping("/userprofile/dim-info-define")
@CrossOrigin
public class DimInfoDefineController {


    @Autowired
    DimInfoDefineService dimInfoDefineService;

    @GetMapping("/list")
    public String getList(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize){
        //分页
        Integer rowNo= (pageNo-1)*pageSize;
        QueryWrapper<DimInfoDefine> queryWrapper = new QueryWrapper<DimInfoDefine>()
                .select("id","dim_name","source_table_name","source_db_name","sink_columns","create_time","update_time")
                .last("limit " + rowNo + "," + pageSize);
        List<DimInfoDefine> eventDefineList = dimInfoDefineService.list(queryWrapper);
        Integer count= dimInfoDefineService.count();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",eventDefineList);
        jsonObject.put("total",count);
        return jsonObject.toJSONString();
    }

    @PostMapping("/detail")
    public String getDetail(@RequestBody DimInfoDefine dimInfoDefine ){

        //更新日期
        if(dimInfoDefine.getId()==null){
            dimInfoDefine.setCreateTime(new Date());
        }else{
            dimInfoDefine.setUpdateTime(new Date());
        }
        dimInfoDefineService.saveOrUpdate(dimInfoDefine);
        return  JSON.toJSONString(dimInfoDefine);
    }

    @GetMapping("/detail/{id}")
    public String getDetail(@PathVariable("id") String id) {
        DimInfoDefine dimInfoDefine = dimInfoDefineService.getById(id);
        return JSON.toJSONString(dimInfoDefine);
    }



    @DeleteMapping("/detail/{id}")
    public String deleteDetail(@RequestBody DimInfoDefine dimInfoDefine) {
        dimInfoDefineService.removeById(dimInfoDefine);
        return "success";
    }


}
